package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理Controller
 *
 * @author guomo
 * @date 2025-10-17
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 同步产品列表
     */
    @ApiOperation("同步产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return AjaxResult.success();
    }

    /**
     * 查询同步产品列表
     */
    @ApiOperation("查询产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/allProduct")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return AjaxResult.success(list);
    }

    /**
     * 查询同步产品列表
     */
    @ApiOperation("查询产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return AjaxResult.success();
    }

    /**
     * 查询设备详细数据
     */
    @ApiOperation("查询设备详细数据")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/{iotId}")
    public AjaxResult getDeviceInfo(@PathVariable("iotId") String iotId) {
        DeviceDetailVo deviceInfo = deviceService.getDeviceInfo(iotId);
        return AjaxResult.success(deviceInfo);
    }

    /**
     * 查看设备上报的数据
     */
    @ApiOperation("查看设备上报的数据")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<DeviceDataVo> deviceDataVoList = deviceService.queryServiceProperties(iotId);
        return AjaxResult.success(deviceDataVoList);
    }

    /**
     * 修改设备
     */
    @ApiOperation("修改设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @PutMapping()
    public AjaxResult updateDevice(@RequestBody DeviceDto dto) {
        deviceService.updateDevice(dto);
        return AjaxResult.success();
    }

    /**
     * 删除设备
     */
    @ApiOperation("删除设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @DeleteMapping("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable("iotId") String iotId) {
        deviceService.deleteDevice(iotId);
        return AjaxResult.success();
    }
    // /queryProduct/{productKey}

    /**
     * 1.9 查询产品详情(报警规则模块使用)
     */
    @ApiOperation("查询产品详情")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProduct(@PathVariable("productKey") String productKey) {
        return deviceService.queryProduct(productKey);
    }
}
